/*
 * @Author: yangzonglong
 * @Date: 2021-02-18 13:53:27
 * @version: v1.0.0
 * @Descripttion: 
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-05-25 08:58:02
 * @Auditor: 
 */
import React from 'react';
import { Popconfirm } from 'antd';
import preDeletePrompt from './../button/preDeletePrompt';
import roleAuth from './../../utils/routePermit/routePermit';

interface IProps {
  type?: string;
  Edit?: React.FC<Record<string, any>>;
  isModalTip?: boolean, // 删除提示方式
  appoint?: string;
  [propName: string]: any;
}

const ActionText = (props: IProps) => {
  const { type, children, ...options } = props;

  return (
    <span className='common-link' {...options}>
      {children || (window as any).$app.t(type)}
    </span>
  );
};

ActionText.Edit = (props: Record<string, any>) => {
  const key = props.appoint || 'edit' || 'view';
  const permit = roleAuth()[key];
  return permit ? <ActionText type={key} {...props} /> : null;
};

ActionText.Detail = (props: Record<string, any>) => {
  return <ActionText type='detail' {...props} />;
};

ActionText.Delete = (props: Record<string, any>) => {
  const permit = roleAuth().delete;

  if (!permit) return null;

  return props.isModalTip ? (
    <div
      onClick={() => preDeletePrompt(props.onClick)}
      className='common-link'>
      {window.$app.t('delete')}
    </div>
  ) : (
    <Popconfirm title={window.$app.t('preDeleteTip')} onConfirm={props.onClick}>
      <div className='common-link'>{window.$app.t('delete')}</div>
    </Popconfirm>
  );
};

ActionText.View = (props: Record<string, any>) => {
  const permit = roleAuth().view;
  return permit ? <ActionText type='view' {...props} /> : null;
};

ActionText.OnlyEdit = (props: Record<string, any>) => {
  const permit = roleAuth().edit;
  return permit ? <ActionText type='edit' {...props} /> : null;
};

export default ActionText;